package app

import (
	"github.com/rodaine/table"
	"github.com/spf13/cobra"

	"gitverse.ru/nickolasfox/kek/pkg/plugins"
)

// Commands prints available commands in a table format on a screen.
type Commands struct {
	Command
}

// NewCommands creates & configures Commands.
func NewCommands(client plugins.Interface) *Commands {
	command := &Commands{
		Command: Command{client: client},
	}
	cmd := &cobra.Command{
		Use:     "commands",
		Aliases: []string{"c", "com", "comm"},
		Short:   "Show available commands",
		RunE:    command.execute,
	}
	command.cmd = cmd
	return command
}

// commands should print available commands.
func (c *Commands) execute(cmd *cobra.Command, _ []string) (err error) {
	tbl := table.New("", "Name", "Description", "Version")
	for _, plugin := range plugins.LoadPlugins() {
		tbl.AddRow("-", plugin.Name, plugin.Description, plugin.Version)
	}
	tbl.Print()
	return
}
